<?php


namespace app\store\controller\fitness;


use app\api\model\User;
use app\store\controller\Controller;
use app\store\model\store\shop\Coach;
use app\common\model\Fitness as FitnessModel;
use think\Config;
use think\Db;

class Plan extends Controller
{
    public function index($user_id){
        $model = new FitnessModel();
        $coachList = $this->coachList();
        $list = $model->getList(array_merge(['type'=>['in','1,6']],$this->request->param()));
        $fitness_type = Config::get('fitness_type');
        $title = "训练计划表数据";
        $add_url = "fitness.plan/add2";
        $edit_url = "fitness.plan/edit";
        return $this->fetch('index', compact('list','coachList','fitness_type','user_id','title','add_url','edit_url'));
    }
    public function add($user_id){
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            $user = User::detail(['user_id'=>$user_id]);
            return $this->fetch('add',compact('coachList','user'));
        }
        $data = $this->postData('fitness');
        $model = new FitnessModel();
        if ($model->add($data,$user_id,1)){
            return  $this->renderSuccess('添加成功',url('fitness.plan/index',['user_id'=>$user_id]));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }

    public function edit($id){
        $detail = FitnessModel::detail(['id'=>$id]);
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            if ($detail['type'] == 1){
                return $this->fetch('edit',compact('coachList','detail'));
            }else{
                return $this->fetch('edit2',compact('coachList','detail'));
            }
        }
        $data = $this->postData('fitness');
        if ($detail->edit($data)){
            return  $this->renderSuccess('保存成功',url('fitness.plan/index',['user_id'=>$detail['user_id']]));
        }
        return $this->renderError($detail->getError()?:'保存失败');
    }


    public function delete($id){
        $model = FitnessModel::detail(['id'=>$id]);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * @title 训练计划新版
     * @param $user_id
     * @author Qian
     * Time: 2023/5/24  9:42
     */
    public function add2($user_id){
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            $user = User::detail(['user_id'=>$user_id]);
            return $this->fetch('add2',compact('coachList','user'));
        }
        $data = $this->postData('fitness');
        $model = new FitnessModel();
        if ($model->add($data,$user_id,6)){
            return  $this->renderSuccess('添加成功',url('fitness.plan/index',['user_id'=>$user_id]));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }
    /**
     * @title 训练计划新版
     * @param $id
     * @author Qian
     * Time: 2023/5/24  9:42
     */
    public function edit2($id){
        $detail = FitnessModel::detail(['id'=>$id]);
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            return $this->fetch('edit2',compact('coachList','detail'));
        }
        $data = $this->postData('fitness');
        if ($detail->edit($data)){
            return  $this->renderSuccess('保存成功',url('fitness.plan/index',['user_id'=>$detail['user_id']]));
        }
        return $this->renderError($detail->getError()?:'保存失败');
    }

    public function coachList(){
        //所属门店的教练
        $shop_id = $this->store['user']['shop_user_id'];
        $where=[];
        if ($shop_id){
            $where['shop_id']=$shop_id;
        }
        $model = new Coach();
        return $model->getAllList($where);
    }
}